/**
 *  编辑页
 *
 * 作者 zhangwei
 * 日期 2024-09-14 10:13:43
 */
layui.use(['jquery','febs', 'form', 'validate','treeSelect','commonJS'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        commonJS = layui.commonJS,
        treeSelect = layui.treeSelect,
        $view = $('#menu-edit'),
        $icon = $view.find('input[name="icon"]'),
        validate = layui.validate;


    var parentId = POPUP_DATA.parentId;
    var parentName = POPUP_DATA.parentName;

    form.verify(validate);
    form.render();
    initFormValue();
    renderTree();
    function initFormValue() {
        if (menuData) {
            form.val("menu-edit-form", {
                "menuId": menuData.menuId,
                "parentId": menuData.parentId,
                "parentName": menuData.parentName,
                "menuName": menuData.menuName,
                "url": menuData.url,
                "perms": menuData.perms,
                "icon": menuData.icon,
                "type": menuData.type,
                "orderNum": menuData.orderNum,
                "status": menuData.status == '1'? true:false,
                "grantedSubsidiaryFlg": menuData.grantedSubsidiaryFlg == '1'? true:false,
            });
            if(menuData.parentId === '0'){
                $view.find('input[name="parentName"]').parents('.layui-form-item').hide();
            }
            if(menuData.type === '1'){
                $view.find('input[name="url"]').parents('.layui-form-item').hide();
                $view.find('input[name="icon"]').parents('.layui-form-item').hide()
            }
        }
    }


    function renderTree()  {
        treeSelect.render({
            elem:'#parentId',
            data:ctx+'menu/treeSelect',
            type:'get',
            placeholder:'请选择上级菜单',
            search:false,
            click:function (d) {
                $view.find('#parentId').val(d.current.id);
                $view.find('input[name="pid"]').val(d.current.id);
            },
            success:function (d) {
                if(menuData){
                    let parentId =  menuData.parentId;
                    if(!commonJS.isEmpty(parentId) && parentId != '0'){
                        treeSelect.checkNode("parentId",parentId)
                    }
                    $view.find('input[name="pid"]').val(parentId);
                }else{
                    if(!commonJS.isEmpty(parentId)){
                        treeSelect.checkNode("parentId",parentId);
                        $view.find('input[name="pid"]').val(parentId);
                    }
                }
            }

        })
    }


    $icon.focus(function () {
        febs.modal.open('图标选择', 'others/febs/icon', {
            btn: ['确定'],
            yes: function (index, layero) {
                var icon = $('#febs-icon').find('.icon-active .icon-name').text();
                if (icon) {
                    form.val("menu-edit-form", {
                        "icon": 'layui-icon-' + icon
                    });
                } else {
                    form.val("menu-edit-form", {
                        "icon": ''
                    });
                }
                layer.close(index);
            }
        });
    });

    form.on("radio(menu-type)", function (data) {
        console.log(data.value);
        form.val("menu-edit-form", {
            "icon": '',
            "url": '',
        });
        if(data.value === '1'){
            $view.find('input[name="url"]').parents('.layui-form-item').hide();
            $view.find('input[name="icon"]').parents('.layui-form-item').hide()
        }else{
            $view.find('input[name="url"]').parents('.layui-form-item').show();
            $view.find('input[name="icon"]').parents('.layui-form-item').show()
        }

    });

    form.on('submit(menu-edit-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        let pid = data.pid;
        data.parentId = pid;
        if(data.status == 'on'){
            data.status = '1';
        }else{
            data.status = '0';
        }
        if(data.grantedSubsidiaryFlg == 'on'){
            data.grantedSubsidiaryFlg = '1';
        }else{
            data.grantedSubsidiaryFlg = '0';
        }
        febs.modal.confirm("保存提示！","是否保存？",function (index){
            if (data.menuId && data.menuId.length > 0) {
                febs.postArray(ctx + 'menu/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    $('#febs-menuNew').find('#query').trigger('click');
                });
            } else {
                febs.postArray(ctx + 'menu', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    $('#febs-menuNew').find('#query').trigger('click');
                });
            }
        })
    }

});